﻿define(function (require, exports) {

    $(function () {
        var appMonth = "";
        var onSelectPageFun = function (page, pageSize) {
            $("#appList tbody").empty();
            var $loadingRow = getLoadRow(12);
            $.ajax({
                url: "../Home/GetAppList",
                data: { appMonth: appMonth, page: page, pageSize: pageSize, counselorId: $("#txtCounselorId").val(), directorId: $("#txtDirectorId").val(), status: $("#selectStatus option:selected").val() },
                dataType: "json",
                beforeSend: function () {
                    $loadingRow.appendTo("#appList tbody");
                },
                type: "post",
                success: function (result) {

                    if (result.length == 0) {
                        var $tr = $("<tr><td colspan=12>暂无数据</td></tr>");
                        $tr.appendTo("#appList tbody");
                        return;
                    }

                    $.each(result, function (index, item) {
                        var vitualPhoneNum = (item.VitualPhone.VitualPhoneNum == "" ? "暂无" : item.VitualPhone.VitualPhoneNum);
                        var status = getStatus(item.Status);
                        var IsEnabledHandelAgain = item.IsEnabledHandelAgain;
                        var id = item.Id;
                        if (IsEnabledHandelAgain) {
                            status += "<br/><span id=" + id + " class =handleAgain>重新处理</span>";
                        }
                        var $tr = $("<tr></tr>");
                        $tr.append("<td>" + id + "</td>");
                        $tr.append("<td>" + item.ApplicationMonth + "</td>");
                        $tr.append("<td>" + item.CounselorId + "</td>");
                        $tr.append("<td>" + item.CounselorName + "</td>");
                        $tr.append("<td>" + item.Org_Name + "</td>");
                        $tr.append("<td>" + item.Mobile + "</td>");
                        $tr.append("<td>" + item.DirectorName + "</td>");
                        $tr.append("<td>" + item.DirectorId + "</td>");
                        $tr.append("<td>" + item.PhoneKind.Name + "</td>");
                        $tr.append("<td>" + vitualPhoneNum + "</td>");
                        $tr.append("<td>" + item.AddDate + "</td>");
                        $tr.append("<td class='tdStatus'>" + status + "</td>");
                        $tr.appendTo("#appList tbody");
                    });
                },
                complete: function () {
                    $loadingRow.remove();
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus + errorThrown);
                }
            });
        };
        var getAppListCount = function (appMonth) {
            var count = 0;
            $.ajax({
                url: "../Home/getAppListCount",
                data: { appMonth: appMonth, counselorId: $("#txtCounselorId").val(), directorId: $("#txtDirectorId").val(), status: $("#selectStatus option:selected").val() },
                async: false,
                type: 'post',
                dataType: "json",
                success: function (result) {
                    count = parseInt(result);
                }
            });
            return count;
        }

        $('#pp').pagination({
            total: getAppListCount(appMonth),
            pageSize: 15,
            onSelectPage: onSelectPageFun,
            showPageList: false,
            beforePageText: "页",
            afterPageText: "/ {pages}",
            displayMsg: "显示{from} 到 {to} 共 {total} 项"
        });

        var getStatus = function (code) {
            var info = "";
            switch (code) {
                case -7: info = "人员离职无需处理";
                    break;
                case -6: info = "同一手机号被多个业务员使用";
                    break;
                case -5: info = "同一业务员申请使用不同手机号";
                    break;
                case -4: info = "系统错误";
                    break;
                case -3: info = "号码已存在";
                    break;
                case -2: info = "同一网站只分配一个虚拟号码";
                    break;
                case -1: info = "无号码可用";
                    break;
                case 0: info = "待处理";
                    break;
                case 1: info = "分配成功";
                    break;
                default: info = "其他";
            }
            return info;
        };
        $("#appClear").click(function () {
            $('#formApp').form('clear');
        });

        $("#appSubmit").click(function () {
            var webId = $("#webNameList option:selected").val();
            var webName = $("#webNameList option:selected").text();
            var counselorId = $("#CounselorId").val();
            var mobile = $("#Mobile").val();
            //var directorId = $("#DirectorId").val(); 目前不在根据用户输入获取总监信息
            if (webId == -99 || webId == undefined) {
                $.messager.alert('提示', '请选择网站类别!');
                return;
            }
            var data = $("#formApp").serialize();
            if (mobile == undefined || mobile == '' || counselorId == undefined || counselorId=='') {
                //alert(mobile + '---' + counselorId + '--');
                $.messager.alert('提示', '信息不完整！');
                return;
            }
            var regMobile = /^[1]+[3-8]\d{9}$/;
            if (!regMobile.test(mobile)) {
                $.messager.alert('提示', '手机号码格式不正确！');
                return;
            }
            $.ajax({
                url: "../Home/SingleHandler",
                data: data,
                dataType: "json",
                type: "post",
                success: function (result) {
                    if (result.Result) {
                        $.messager.alert('提示', result.Message);
                    } else {
                        $.messager.alert('提示', result.Exception);
                    }
                }
            });
        });

        $("#webNameList").change(function () {
            var webId = $("#webNameList option:selected").val();
            var webName = $("#webNameList option:selected").text();
            $("#webId").val(webId);
            $("#webName").val(webName);
        });

        var loadwebNameList = function () {
            $.ajax({
                url: "../Home/GetWebList",
                dataType: "json",
                success: function (result) {
                    $.each(result, function (index, item) {
                        var option = $("<option value=" + item.Id + ">" + item.Name + "</option>");
                        $("#webNameList").append(option);
                    });
                }
            });
        }

        loadwebNameList(); //加载下拉框

        $("#btnSearch").click(function () {
            onSelectPageFun(1, 15);
            $('#pp').pagination({
                total: getAppListCount(appMonth),
                pageSize: 15,
                onSelectPage: onSelectPageFun,
                showPageList: false,
                beforePageText: "页",
                afterPageText: "/ {pages}",
                displayMsg: "显示{from} 到 {to} 共 {total} 项"
            });
        });
        $(".handleAgain").live("click", function () {
            var id = $(this).attr("id");
            $.messager.confirm('提示', '确定要重新处理这条申请吗？', function (r) {
                if (r) {
                    $.ajax({
                        url: "../Home/SingleHandlerWithId",
                        data: { id: id },
                        dataType: "json",
                        type: "post",
                        success: function (result) {
                            if (result.Result) {
                                $.messager.alert('提示', result.Message);
                            } else {
                                $.messager.alert('提示', result.Exception);
                            }
                        }
                    });
                }
            });
        });
        onSelectPageFun(1, 15, '', ''); //加载首页数据
        $("#searchVitualPhoneList").click(function () {
            var actualNo = $("#txtactualNo").val();
            var regMobile = /^[1]+[3-8]\d{9}$/;
            if (!regMobile.test(actualNo)) {
                $.messager.alert('提示', '手机号码格式不正确！');
                return;
            }
            //
            $("#vitualPhoneList tbody").empty();
            $.ajax({
                url: "../Home/GetPhoneListFromTdialnumber",
                data: { actualNo: actualNo },
                dataType: "json",
                type: "post",
                success: function (result) {
                    if (result.length == 0) {
                        var $tr = $("<tr><td colspan=4>暂无数据</td></tr>");
                        $tr.appendTo("#vitualPhoneList tbody");
                        return;
                    }
                    $.each(result, function (index, item) {
                        var $tr = $("<tr></tr>");
                        $tr.append("<td>" + item.ActulPhoneNum + "</td>");
                        $tr.append("<td>" + item.VitualPhoneNum + "</td>");
                        $tr.append("<td>" + item.PhoneKind.Name + "</td>");
                        $tr.append("<td><input  class='checkVP' type='checkbox' VitualPhoneNum=" + item.VitualPhoneNum + " /></td>");
                        $tr.appendTo("#vitualPhoneList tbody");
                    });
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus + errorThrown);
                }
            })
            //

        });

        $("#btnRealse").click(function () {
            var vitualPhoneNumList = '';
            $.each($(".checkVP:checked"), function (index, item) {
                var vp = $(this).attr("VitualPhoneNum");
                if (index == 0) {
                    vitualPhoneNumList += vp;
                } else {
                    vitualPhoneNumList += ',' + vp;
                }
            });
            if (vitualPhoneNumList == '') {
                $.messager.alert('提示', '请勾选要解绑的手机号码！');
            }
            else {
                $.messager.confirm('提示', '确定要解绑手机号码' + vitualPhoneNumList, function (r) {
                    if (r) {
                        $.ajax({
                            url: "../Home/ReleaseNum",
                            data: { vitualPhoneNumList: vitualPhoneNumList },
                            dataType: "json",
                            success: function (result) {
                                if (result) {
                                    $.messager.alert('提示', '解绑成功');
                                    $("#searchVitualPhoneList").click(); //再次获取数据
                                } else {
                                    $.messager.alert('提示', '解绑失败');
                                    $("#searchVitualPhoneList").click();//再次获取数据
                                }
                            }
                        });
                    }
                });
            }

        });

    });
});